﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;

namespace BLL
{
    public class PJOrderStartManager
    {
        private PJOrderStartService objPJOrderStartService = new PJOrderStartService();

        public string InsertIsNoSucess(PJOrderStart main)
        {
            string result;
            string serialNum = GetLastSerialNum(main.OrderType, main.OrderPurpose);
            if(!string.IsNullOrEmpty(serialNum))
            {
                int temp = int.Parse(serialNum.Substring(3, 5));
                temp++;
                serialNum = "1" + main.SerialNum + temp.ToString().PadLeft(5, '0');
                main.SerialNum = serialNum;
                foreach (var item in main.ChildList)
                {
                    item.SerialNum = main.SerialNum;
                }
            }
            else
            {
                main.SerialNum = "1" + main.SerialNum + "00001";
                foreach (var item in main.ChildList)
                {
                    item.SerialNum = main.SerialNum;
                }
            }
            int a = IsNoOrderStart(main.WorkParentId);
            if(a==0)
            {
                bool msg = InsertInfo(main);
                if (msg == true)
                {
                    result = main.SerialNum;
                }
                else
                {
                    result = "false";
                }
            }
            else
            {
                result = "false";
            }
            
            return result;
        }
        public int UpdateDate(DateTime confirmDate,string matnr, string serialNum, int row)
        {
            return objPJOrderStartService.UpdateDate(confirmDate, matnr, serialNum, row);
        }
        
        public int UpdateSapOrder(string sapOrder, string serialNum, int row)
        {
            return objPJOrderStartService.UpdateSapOrder(sapOrder, serialNum, row);
        }
        public int UpdateStatues(int workParentId)
        {
            return objPJOrderStartService.UpdateStatues(workParentId);
        }
        private bool InsertInfo(PJOrderStart main)
        {
            return objPJOrderStartService.InsertInfo(main);
        }
        public void UpdateInfo(PJOrderStart main)
        {
            foreach (var item in main.ChildList)
            {
                item.SerialNum = main.SerialNum;
            }
            objPJOrderStartService.UpdateInfo(main);
            int childListNum = objPJOrderStartService.GetChildListNum(main.SerialNum);
            if(childListNum<main.ChildList.Count)
            {
                main.ChildList = main.ChildList.Where(s => s.Row > childListNum).ToList();
                //添加新行项目
                objPJOrderStartService.InsertChildInfo(main);
            }
            else if(childListNum>main.ChildList.Count)
            {
                objPJOrderStartService.DelChildList(main.SerialNum, main.ChildList.Count);
                //删除旧行项目
            }
        }
        private string GetLastSerialNum(string orderType, string orderPurpose)
        {
            return objPJOrderStartService.GetLastSerialNum(orderType,orderPurpose);
        }
        public PJOrderStart GetOrderStart(int workParentId)
        {
            return objPJOrderStartService.GetOrderStart(workParentId);
        }
        public int IsNoOrderStart(int workParentId)
        {
            return objPJOrderStartService.IsNoOrderStart(workParentId);
        }
    }
}
